<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Class	: M_terimabarangdet
 * 
 * Table	: terimabarangdet
 *  
 * @author Eko Junaidi Salam
 *
 */
class M_terimabarangdet extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	/**
	 * Fungsi	: getAll
	 * 
	 * Untuk mengambil all-data
	 * 
	 * @param number $start
	 * @param number $page
	 * @param number $limit
	 * @return json
	 */
	function getAll($no_lpb,$start, $page, $limit){
		$query  = $this->db->limit($limit, $start)->order_by('ID', 'DESC')->get_where('terimabarangdet',array('NOMOR_LPB'=>$no_lpb))->result();
		$total  = $this->db->get_where('terimabarangdet',array('NOMOR_LPB'=>$no_lpb))->num_rows();
		
		$data   = array();
		foreach($query as $result){
			$data[] = $result;
		}
		
		$json	= array(
						'success'   => TRUE,
						'message'   => "Loaded data",
						'total'     => $total,
						'data'      => $data
		);
		
		return $json;
	}
	
	function UpdateTotal($data)
	{
		// Update Keseluruhan Data Detail untuk Sub Total
		$det = $this->db->get_where('terimabarangdet',array('NOMOR_LPB'=>$data->NOMOR_LPB))->result();
		$sub_total = 0;
		foreach($det as $val)
		{
			$sub_total += $val->HARGA_TOTAL;
		}
		
		// Perhitungan Materai
		/* < 250rb = 0
		>=250rb <1jt = 3000
		>=1jt = 6000 */
		$ppn = ($sub_total*0.1);
		$materai = ($sub_total >= 250000 && $sub_total < 1000000?3000:($sub_total >= 1000000?6000:0));
		$hasil = $sub_total + $ppn + $materai;
		
		// $this->firephp->info($sub_total);
		$this->db->where('NOMOR_LPB',$data->NOMOR_LPB)->update('terimabarang', array('SUB_TOTAL' => $sub_total));
		$this->db->where('NOMOR_LPB',$data->NOMOR_LPB)->update('terimabarang', array('PPN' => $ppn));
		$this->db->where('NOMOR_LPB',$data->NOMOR_LPB)->update('terimabarang', array('MATERAI' => $materai));
		$this->db->where('NOMOR_LPB',$data->NOMOR_LPB)->update('terimabarang', array('TOTAL' => $hasil));
	}
	
	/**
	 * Fungsi	: save
	 * 
	 * Untuk menambah data baru atau mengubah data lama
	 * 
	 * @param array $data
	 * @return json
	 */
	function save($data){
		$last   = NULL;
		
		$pkey = array('NOMOR_LPB'=>$data->NOMOR_LPB,'KODE_BARANG'=>$data->KODE_BARANG);
		
		if($this->db->get_where('terimabarangdet', $pkey)->num_rows() > 0){
			/*
			 * Data Exist
			 */			 
			/* $tmp = substr($data->HARGA_SATUAN,3,strlen($data->HARGA_SATUAN));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->HARGA_SATUAN = $tmp;$tmp = substr($data->HARGA_TOTAL,3,strlen($data->HARGA_TOTAL));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->HARGA_TOTAL = $tmp;	 */
			 
			$arrdatau = array('NOMOR_PO'=>$data->NOMOR_PO,'TGL_DETAIL_LPB'=>(strlen(trim($data->TGL_DETAIL_LPB)) > 0 ? date('Y-m-d', strtotime($data->TGL_DETAIL_LPB)) : NULL),'NAMA_BARANG'=>$data->NAMA_BARANG,'BATCH_NO'=>$data->BATCH_NO,'QUANTITY'=>$data->QUANTITY,'SATUAN'=>$data->SATUAN,'HARGA_SATUAN'=>$data->HARGA_SATUAN,'HARGA_TOTAL'=>$data->HARGA_TOTAL,'BEA_MASUK'=>$data->BEA_MASUK,'NOTE'=>$data->NOTE);
			 
			$this->db->where($pkey)->update('terimabarangdet', $arrdatau);
			$last   = $data;
			
			// Update Keseluruhan Data Detail untuk Sub Total
			/* $det = $this->db->get_where('terimabarangdet',array('NOMOR_LPB'=>$data->NOMOR_LPB))->result();
			$sub_total = 0;
			foreach($det as $val)
			{
				$sub_total += $val->HARGA_TOTAL;
			}
			
			// Perhitungan Materai
			// < 250rb = 0
			// >=250rb <1jt = 3000
			// >=1jt = 6000
			$ppn = ($sub_total*0.1);
			$materai = ($sub_total >= 250000 && $sub_total < 1000000?3000:($sub_total >= 1000000?6000:0));
			$total = $sub_total + $ppn + $materai;
			
			// $this->firephp->info($sub_total);
			$this->db->where('NOMOR_LPB',$data->NOMOR_LPB)->update('terimabarang', array('SUB_TOTAL' => $sub_total));
			$this->db->where('NOMOR_LPB',$data->NOMOR_LPB)->update('terimabarang', array('PPN' => $ppn));
			$this->db->where('NOMOR_LPB',$data->NOMOR_LPB)->update('terimabarang', array('MATERAI' => $materai));
			$this->db->where('NOMOR_LPB',$data->NOMOR_LPB)->update('terimabarang', array('TOTAL' => $total)); */
			
		}else{
			/*
			 * Data Not Exist
			 * 
			 * Process Insert
			 */
			/* $tmp = substr($data->HARGA_SATUAN,3,strlen($data->HARGA_SATUAN));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->HARGA_SATUAN = $tmp;$tmp = substr($data->HARGA_TOTAL,3,strlen($data->HARGA_TOTAL));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->HARGA_TOTAL = $tmp; */
			
			$arrdatac = array('NOMOR_LPB'=>$data->NOMOR_LPB,'NOMOR_PO'=>$data->NOMOR_PO,'TGL_DETAIL_LPB'=>(strlen(trim($data->TGL_DETAIL_LPB)) > 0 ? date('Y-m-d', strtotime($data->TGL_DETAIL_LPB)) : NULL),'KODE_BARANG'=>$data->KODE_BARANG,'NAMA_BARANG'=>$data->NAMA_BARANG,'BATCH_NO'=>$data->BATCH_NO,'QUANTITY'=>$data->QUANTITY,'SATUAN'=>$data->SATUAN,'HARGA_SATUAN'=>$data->HARGA_SATUAN,'HARGA_TOTAL'=>$data->HARGA_TOTAL,'BEA_MASUK'=>$data->BEA_MASUK,'NOTE'=>$data->NOTE,'ID'=>$data->ID);
			 
			$this->db->insert('terimabarangdet', $arrdatac);			
			$last   = $this->db->where($pkey)->get('terimabarangdet')->row();
			
		}
		$this->UpdateTotal($data);
		$total  = $this->db->get('terimabarangdet')->num_rows();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil disimpan',
						'total'     => $total,
						"data"      => $last
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: delete
	 * 
	 * Untuk menghapus satu data
	 * 
	 * @param array $data
	 * @return json
	 */
	function delete($data){
		$pkey = array('NOMOR_LPB'=>$data->NOMOR_LPB,'KODE_BARANG'=>$data->KODE_BARANG);
		
		$this->db->where($pkey)->delete('terimabarangdet');
		
		$total  = $this->db->get('terimabarangdet')->num_rows();
		$last = $this->db->get('terimabarangdet')->result();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil dihapus',
						'total'     => $total,
						"data"      => $last
		);				
		return $json;
	}
}
?>